package com.xbts.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.system.domain.PartyArea;
import com.xbts.system.domain.PartyPointsDeclarationStatistics;
import com.xbts.system.mapper.PartyAreaMapper;
import com.xbts.system.service.IPartyAreaService;
import com.xbts.system.service.IPartyPointsDeclarationStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 片区管理Service业务层处理
 *
 * @author xbts
 * @date 2023-06-12
 */
@Service
public class PartyAreaServiceImpl extends ServiceImpl<PartyAreaMapper, PartyArea> implements IPartyAreaService {

    @Autowired
    private IPartyPointsDeclarationStatisticsService partyPointsDeclarationStatisticsService;

    /**
     * 查询片区管理列表
     *
     * @param partyArea 片区管理
     * @return 片区管理
     */
    @Override
    public List<PartyArea> selectPartyAreaList(PartyArea partyArea) {
        QueryWrapper<PartyArea> queryWrapper = partyArea.queryWrapper();
        queryWrapper.orderByDesc("create_time");
        return list(queryWrapper);
    }

    /**
     * 查询片区管理
     *
     * @param areaId 片区管理主键
     * @return 片区管理
     */
    @Override
    public PartyArea selectPartyAreaByAreaId(String areaId) {
        return getById(areaId);
    }

    /**
     * 新增片区管理
     *
     * @param partyArea 片区管理
     * @return 结果
     */
    @Override
    public boolean insertPartyArea(PartyArea partyArea) {
        // 判断片区名称是否重复
        LambdaQueryWrapper<PartyArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PartyArea::getAreaName, partyArea.getAreaName()).or();
        queryWrapper.eq(PartyArea::getAreaCode, partyArea.getAreaCode());
        if (count(queryWrapper) > 0) {
            throw new ServiceException("新增片区【" + partyArea.getAreaName() + "】失败，片区名称或片区编码已存在");
        }
        partyArea.setCreateBy(SecurityUtils.getUserId().toString());
        partyArea.setCreateTime(DateUtils.getNowDate());
        return save(partyArea);
    }

    /**
     * 修改片区管理
     *
     * @param partyArea 片区管理
     * @return 结果
     */
    @Override
    public boolean updatePartyArea(PartyArea partyArea) {
        // 判断片区名称是否重复
        QueryWrapper<PartyArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PartyArea::getAreaName, partyArea.getAreaName());
        queryWrapper.lambda().ne(PartyArea::getAreaId, partyArea.getAreaId());
        if (count(queryWrapper) > 0) {
            throw new ServiceException("修改片区【" + partyArea.getAreaName() + "】失败，片区名称已存在");
        }
        QueryWrapper<PartyArea> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(PartyArea::getAreaCode, partyArea.getAreaCode());
        queryWrapper2.lambda().ne(PartyArea::getAreaId, partyArea.getAreaId());
        if (count(queryWrapper2) > 0) {
            throw new ServiceException("修改片区【" + partyArea.getAreaName() + "】失败，片区编码已存在");
        }
        partyArea.setUpdateBy(SecurityUtils.getUserId().toString());
        partyArea.setUpdateTime(DateUtils.getNowDate());
        return updateById(partyArea);
    }

    /**
     * 批量删除片区管理
     *
     * @param areaIds 需要删除的片区管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePartyAreaByAreaIds(List<String> areaIds) {
        // 删除排行榜中地区数据
        LambdaQueryWrapper<PartyPointsDeclarationStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PartyPointsDeclarationStatistics::getBelongToArea, areaIds);
        partyPointsDeclarationStatisticsService.remove(queryWrapper);

        return removeBatchByIds(areaIds);
    }

    /**
     * 删除片区管理信息
     *
     * @param areaId 片区管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deletePartyAreaByAreaId(String areaId) {
        return removeById(areaId);
    }

}
